package lv.solcraft.replication.server.boot;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Options;

public class StartupOptions {

    private static final String HTTP_LOG = "httpLog";
    private static final String HTTP_PORT = "httpPort";
    private static final String REPL_HOME = "replHome";
    private String replHome = ".";
    private int httpPort = 8080;
    private boolean httpLog = true;

    public String getReplHome() {
        return replHome;
    }

    public int getHttpPort() {
        return httpPort;
    }

    public static StartupOptions parse(CommandLine commandLine) {
        StartupOptions options = new StartupOptions();
        options.replHome = commandLine.getOptionValue(REPL_HOME, ".");
        options.httpPort = Integer.parseInt(commandLine.getOptionValue(HTTP_PORT, "8080"));
        options.httpLog = Boolean.parseBoolean(commandLine.getOptionValue(HTTP_LOG, "true"));
        return options;
    }

    public static StartupOptions parse(String... args) throws Exception {
        CommandLineParser parser = new GnuParser();
        Options options = new Options();
        options.addOption(REPL_HOME, true, "Replication home path");
        options.addOption(HTTP_PORT, true, "Http port");
        options.addOption(HTTP_LOG, true, "Http access logging on/off");
        CommandLine commandLine = parser.parse(options, args);
        return parse(commandLine);
    }

    public boolean isHttpLog() {
        return httpLog;
    }
}
